﻿using DevTeam;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class ToplarinYeri : XPopupForm
    {
        int CikisEmriId;
        string OrderRef;
        public ToplarinYeri(int _CikisEmriId)
        {
            InitializeComponent();
            CikisEmriId = _CikisEmriId;
        }

        public ToplarinYeri(string _OrderRef)
        {
            InitializeComponent();
            OrderRef = _OrderRef;
        }

        private void ToplarinYeri_Load(object sender, EventArgs e)
        {
            //this.Paint +=new PaintEventHandler(ToplarinYeri_Paint);

            DataTable dt = Session.DataManager.ExecuteDataTable("GetCikilacakToplar", CikisEmriId, OrderRef);
            foreach (DataRow r in dt.Rows)
            {
                RackAdet.Add(intParse(r["RackNr"]), intParse(r["Adet"]));
                Invalidate(GetLocation(intParse(r["RackNr"])));
            }
            
            //Invalidate();
        }

        int AisleW = 50,
            RackW = 30,
            RackH = 30,
            Offset = 20;

        double ScaleH = 1,
            ScaleV = 1;

        Dictionary<int, int> RackAdet = new Dictionary<int, int>();
        SolidBrush semiRed2 = new SolidBrush(Color.FromArgb(180, 255, 0, 0));


        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.CompositingQuality = CompositingQuality.HighQuality;

            foreach (KeyValuePair<int, int> p in RackAdet)
            {
                if (p.Key < 1000)
                {
                    Rectangle rct = GetLocation(p.Key);
                    g.FillRectangle(semiRed2, new Rectangle(rct.X, rct.Y, rct.Width, rct.Width));
                    g.DrawString(RackAdet[p.Key].ToString(),
                        new Font("Calibri", 10, FontStyle.Bold),
                        new SolidBrush(Color.DarkRed),
                        rct.X + 10, rct.Y + rct.Height / 2 - 16);
                    g.DrawString(p.Key.ToString(),
                        new Font("Calibri", 9),
                        new SolidBrush(Color.DarkRed),
                        rct.X + 6, rct.Y + rct.Height / 2 + 2);
                }
            }

            //g.DrawString(String.Format("{0:#,###}", ToplamAdet), new Font("Calibri", 56), new SolidBrush(Color.Black), 1000, 100);
            //g.DrawString(String.Format("{0:p2}", ToplamAdet / 23e3), new Font("Calibri", 22), new SolidBrush(Color.Black), 1200, 100);

        }


        Rectangle GetLocation(int x)
        {
            int Aisle = x / 100;
            int Rack = x % 100;
            Rectangle r = new Rectangle();
            r.X = (9 - Aisle) / 2 * (AisleW + 2 * RackW) + ((Aisle - 1) % 2) * RackW;
            r.Y = (11 - Rack) * RackH + (Rack < 6 ? AisleW : 0);
            r.Width = RackW;
            r.Height = RackH;

            r.X = (int)(r.X * ScaleH) + Offset;
            r.Y = (int)(r.Y * ScaleV) + Offset;
            r.Width = (int)(r.Width * ScaleH);
            r.Height = (int)(r.Height * ScaleV);

            return r;
        } 
    }
}